package com.fernfx.xingtan.my.ui;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.entity.user.UserInfoEntity;
import com.fernfx.xingtan.common.verifycode.VerifyCodeSend;
import com.fernfx.xingtan.my.contract.UpdatePNumberContract;
import com.fernfx.xingtan.my.presenter.UpdatePNumberPresenter;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.RegexUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

import static com.fernfx.xingtan.my.ui.AccountSafeActivity.UPDATE_PNUMBER_STATUS;

/**
 * Created by admin on 2018/8/15.
 * 修改手机号
 */

public class UpdatePNumberActivity extends BaseActivity implements View.OnClickListener,UpdatePNumberContract.View{

    @BindView(R.id.old_sms_code_input_edit)
    EditText oldSmsCodeEdit;
    @BindView(R.id.get_old_sms_code_tv)
    TextView getOldSmsCodeTv;
    @BindView(R.id.old_pnumber_edit)
    EditText oldPnumberEdit;
    @BindView(R.id.new_pnumber_edit)
    EditText newPnumberEdit;
    @BindView(R.id.get_new_sms_code_tv)
    TextView getNewSmsCodeTv;
    @BindView(R.id.new_sms_code_input_edit)
    EditText newSmsCodeEdit;

    private CountDownTimer oldCountDownTimer,newCountDownTimer;
    //presenter
    private UpdatePNumberContract.Presenter presenter;
    //短信验证码
    private VerifyCodeSend verifyCodeSend;
    //旧手机号
    private String oldPNumber;

    public static void start(BaseActivity context) {
        Intent intent = new Intent(context,UpdatePNumberActivity.class);
        context.startActivityForResult(intent,UPDATE_PNUMBER_STATUS);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_setting_update_pnumber;
    }

    @Override
    protected void initView() {
        //init presenter
        presenter = new UpdatePNumberPresenter();
        presenter.init(this);
        //设置标题名
        setToolbarTitle("更换手机号");
    }

    @Override
    protected void initData() {
        //获得手机号码并设置
        UserInfoEntity userInfoEntity = AccountManager.getUserInfo().getObj();
        oldPNumber = userInfoEntity.getMobile();
        oldPnumberEdit.setText(oldPNumber);
        //初始化两个倒计时
        oldCountDownTimer = presenter.getTimeCount(oldSmsCodeEdit,getOldSmsCodeTv);
        //新的短信验证码倒计时
        newCountDownTimer = presenter.getTimeCount(newPnumberEdit,getNewSmsCodeTv);
        //初始化短信验证码
        verifyCodeSend = new VerifyCodeSend(this,"changeMobile");
    }

    @OnClick({R.id.get_old_sms_code_tv,R.id.get_new_sms_code_tv,R.id.update_pnumber_bt,R.id.update_pnumber_cle})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.update_pnumber_cle:
                SoftInputUtil.hideKeyboard(this);
                break;
            case R.id.get_old_sms_code_tv:
                String oldPnumberText = trimEditText(oldPnumberEdit);
                oldCountDownTimer.start();
                verifyCodeSend.send(oldPnumberText);
                break;
            case R.id.get_new_sms_code_tv:
                String newPnumberText = trimEditText(newPnumberEdit);
                if (!RegexUtil.isMobileNumber(newPnumberText)) {
                    ToastUtil.showCentertoast("请输入正确的手机号");
                    return;
                }
                newCountDownTimer.start();
                verifyCodeSend.send(newPnumberText);
                break;
            case R.id.update_pnumber_bt:
               String oldSmsText = trimEditText(oldSmsCodeEdit);
                //验证旧的短信验证码
                if (!RegexUtil.isSms(oldSmsText)) {
                    ToastUtil.showCentertoast("验证码格式错误");
                    return;
                }
                String newSmsText = trimEditText(newSmsCodeEdit);
                if (!RegexUtil.isSms(newSmsText)) {
                    ToastUtil.showCentertoast("验证码格式错误");
                    return;
                }
                //输入的新手机号
                String inputPnumberText = trimEditText(newPnumberEdit);
                if (oldPNumber.equals(inputPnumberText)) {
                    ToastUtil.showCentertoast("手机号码一样");
                    return;
                }
                requestArgsMap.clear();
                requestArgsMap.put("newMobile",inputPnumberText);
                requestArgsMap.put("newSmsCode",newSmsText);
                requestArgsMap.put("oldSmsCode",oldSmsText);
                //设置手机号方便更新
                presenter.setNewPnumber(inputPnumberText);
                presenter.request(requestArgsMap);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != presenter) {
            presenter.detachView();
            presenter = null;
        }
        if (null != oldCountDownTimer){
            oldCountDownTimer.cancel();
            oldCountDownTimer = null;
        }
        if (null != newCountDownTimer) {
            newCountDownTimer.cancel();
            newCountDownTimer = null;
        }
        if (null != verifyCodeSend) {
            verifyCodeSend = null;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }
}
